package com.tushu.view.student;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.tushu.dao.BookDao;
import com.tushu.utils.DataUtils;

import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.BevelBorder;

public class SearchBook extends JPanel {
	private JTextField jt;
	private JTable table;
	BookDao bookDao=new BookDao();

	String[] headerString = {"序号","ID","ISBN","书名","作者","出版社","库存"};
	
	String[][] dataString =null;

	String sql1="SELECT * from tb_book where isbn=?";
	String sql2="SELECT * from tb_book where book_name=?";
	String sql3="SELECT * from tb_book";

	/**
	 * Create the panel.
	 */
	public SearchBook() {
		setLayout(null);
		
		JButton jb_search = new JButton("\u67E5\u8BE2");

		jb_search.setBounds(372, 25, 97, 23);
		add(jb_search);
		
		JButton jb_reset = new JButton("\u91CD\u7F6E");
		jb_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jt.setText("");
				getAllBooks();
			}
		});
		jb_reset.setBounds(63, 25, 97, 23);
		add(jb_reset);
		
		jt = new JTextField();
		jt.setBounds(170, 26, 183, 21);
		add(jt);
		jt.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("\u65B9\u5F0F\uFF1A");
		lblNewLabel.setBounds(168, 57, 58, 15);
		add(lblNewLabel);
		
		JRadioButton rb1 = new JRadioButton("ISBN",true);
		rb1.setBounds(211, 55, 66, 23);
		add(rb1);
		
		JRadioButton rb2 = new JRadioButton("\u4E66\u540D");
		rb2.setBounds(279, 54, 127, 23);
		add(rb2);
		
		
		
		ButtonGroup group=new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 89, 506, 223);
		add(scrollPane);
	
		
//		table = new JTable(dataString,headerString);
		table=new JTable();
		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		scrollPane.setViewportView(table);
		
		JButton btnNewButton = new JButton("\u5237\u65B0");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getAllBooks();
			}
		});
		btnNewButton.setBounds(10, 67, 71, 23);
		add(btnNewButton);

		
		getAllBooks();
		
		jb_search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String textString=jt.getText();
				System.out.println(textString);
				if ("".equals(textString)) {
					JOptionPane.showMessageDialog(null, "请输入要查询的内容！");
				}
				else if (rb1.isSelected()) {
					try {
						ResultSet set=DataUtils.query(sql1,new Object[] {textString});
						if (!set.next()) {
							JOptionPane.showMessageDialog(null, "没有找到ISBN为"+textString+"的书籍");
						}else {
							show(set);
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}else if (rb2.isSelected()) {
					try {
						ResultSet set=DataUtils.query(sql2,new Object[] {textString});
						if (!set.next()) {
							JOptionPane.showMessageDialog(null, "没有找到《"+textString+"》这本书籍！");
						}else {
							show(set);
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});

	}

	public void getAllBooks() {
		try {
			ResultSet set=DataUtils.query(sql3, new Object[] {});
			show(set);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void show(ResultSet set) throws SQLException {
		table.setModel(new DefaultTableModel(DataUtils.getSetArrays(set),headerString));
	}
}
